#!/bin/bash
set -e

echo "=========================================="
echo "YOLO11 推理服务启动（NPU版本）"
echo "=========================================="

# 验证环境
echo "验证 Python 环境..."
python3 --version

# 检查NPU设备（如果存在）
echo "检查NPU设备..."
if [ -d "/dev/davinci0" ] || [ -c "/dev/davinci0" ]; then
    echo "✓ 检测到NPU设备"
    ls -l /dev/davinci* 2>/dev/null || echo "  无法列出NPU设备详情"
else
    echo "⚠️  未检测到NPU设备（将使用CPU推理）"
fi

echo "验证 Ultralytics..."
python3 -c "import ultralytics; print(f'Ultralytics 版本: {ultralytics.__version__}')" || {
    echo "错误: Ultralytics 不可用"
    exit 1
}

echo "验证 OpenCV..."
python3 -c "import cv2; print(f'OpenCV 版本: {cv2.__version__}')" || {
    echo "错误: OpenCV 不可用"
    exit 1
}

# 打印配置信息
echo "=========================================="
echo "配置信息:"
echo "  推理设备: NPU (如可用) / CPU (回退)"
echo "  模型目录: ${LOCAL_MODEL_PATH:-未设置}"
echo "  日志级别: ${LOG_LEVEL:-INFO}"
echo "  端口: ${PORT:-8000}"
echo "  NPU设备: ${ASCEND_VISIBLE_DEVICES:-未设置}"
echo "=========================================="

# 创建必要的目录
mkdir -p /app/models /app/logs /app/result

# 启动应用
echo "启动 YOLO11 API 服务（NPU版本）..."
exec python3 -m uvicorn api.main:app --host 0.0.0.0 --port ${PORT:-8000} --app-dir /app

